package com.sy.service.impl;

import com.sy.dto.MemberDto;
import com.sy.entity.Member;
import com.sy.exception.BusinessException;
import com.sy.mapper.MemberMapper;
import com.sy.service.MemberService;
import com.sy.vo.MemberVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.DEFAULT_PICTURE;

/**
 * description: MemberService <br>
 * date: 2022/6/24 12:10 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

@Service
public class MemberServiceImpl extends AbstractBaseServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<Member> getAllMembers() {
        return memberMapper.selectAllMembers();
    }

    @Override
    public Boolean registerMember(MemberVo memberVo) {
        if (StringUtils.isBlank(memberVo.getUserName())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户名不能为空");
        }
        Member member = memberMapper.selectMemberByUserName(memberVo);
        throwIfNotNull(member, "用户名已存在");
        if (StringUtils.isBlank(memberVo.getPassword())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "密码不能为空");
        }
        throwIfFalse(memberVo.getPassword().equals(memberVo.getPwdConfirm()), "两次密码不相等");
        if (StringUtils.isBlank(memberVo.getPhoto())) {
            memberVo.setPhoto(DEFAULT_PICTURE);
        }
        Boolean aBoolean = memberMapper.registerMember(memberVo);
        throwIfFalse(aBoolean, "注册失败");
        return aBoolean;
    }

    @Override
    public MemberDto login(MemberVo memberVo, String correctValidateCode) {
        System.out.println(correctValidateCode);
        throwIfBlank(memberVo.getUserName(), "用户名不能为空");
        throwIfBlank(memberVo.getPassword(), "密码不能为空");
        throwIfBlank(memberVo.getValidateCode(), "验证码不得为空");
        throwIfStringsNotEqualsIgnoreCase(correctValidateCode, memberVo.getValidateCode(), "验证码不正确");
        MemberDto member = memberMapper.login(memberVo);
        throwIfNull(member, "用户不存在");
        return member;
    }


    @Override
    public Boolean updateMember(Member member) {
        if (Objects.isNull(member.getId())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户异常");
        }
        if (StringUtils.isAllBlank(member.getUserName(), member.getPassword(), member.getPhoto(), member.getPayPassword())
                && Objects.isNull(member.getUserPhone()) && Objects.isNull(member.getBalance()) && Objects.isNull(member.getSexId())
                && Objects.isNull(member.getBirth())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户未更新");
        }
        Boolean aBoolean = memberMapper.updateMember(member);
        return aBoolean;
    }

    @Override
    public MemberDto getMemberById(Integer id) {
        if (Objects.isNull(id)) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户异常");
        }
        MemberDto member = memberMapper.selectMemberById(id);
        throwIfNull(member, "用户不存在");
        return member;
    }
}
